﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XHCTMS.VIEW
{
    public partial class Checking_Show : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["ClassId"] = 1304;
                ViewState["PageIndex"] = 1;
                ViewState["PageSize"] = 5;
                DataTable dtPage = new BLL.Checking().GetCountPage(Convert.ToInt32(ViewState["ClassId"]), null);
                int size = Convert.ToInt32(dtPage.Rows[0][0].ToString());
                ViewState["Count"] = size;
                if (size / ChangePageSize() < 1)
                {
                    ViewState["MaxPage"] = 1;
                }
                else
                {
                    ViewState["MaxPage"] = size % ChangePageSize() == 0 ? size / ChangePageSize() : size / ChangePageSize() + 1;
                }                
                PageChange(true);
                ShowData(Convert.ToInt32(ViewState["PageSize"]), 1);          
            }
        }       

        // 上一页        
        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            Pagination(1);
        }


        // 下一页        
        protected void btnNext_Click(object sender, EventArgs e)
        {
            Pagination(0);
        }


        // 首页        
        protected void btnFirst_Click(object sender, EventArgs e)
        {
            Pagination(2);
        }


        // 尾页        
        protected void btnLast_Click(object sender, EventArgs e)
        {
            Pagination(3);
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
           
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(this.ddlPage.SelectedValue.Split('/')[0]);
            
            int pageSize = ChangePageSize();
            ViewState["PageIndex"] = pageIndex;             
            ShowData(pageSize, pageIndex);    
        }

        private void Pagination(int state)
        {
            int pageSize = ChangePageSize();
            int pageIndex = 1;            
            switch (state)
            {
                //下一页
                case 0:
                    pageIndex = ChangePageIndex() + 1;
                    int maxPage = Convert.ToInt32(ViewState["MaxPage"]);
                    this.ddlPage.SelectedIndex += 1;
                    break;
                //上一页
                case 1:
                    pageIndex = ChangePageIndex() - 1;
                    this.ddlPage.SelectedIndex -= 1;
                    break;
                //首页
                case 2:
                    pageIndex = 1;
                    this.ddlPage.SelectedIndex = pageIndex - 1;
                    break;
                //尾页
                case 3:
                    pageIndex = Convert.ToInt32(new BLL.Teaching().GetCountPage(pageSize));
                    this.ddlPage.SelectedIndex = pageIndex - 1;
                    break;
                default:
                    break;
            }

            ViewState["PageIndex"] = pageIndex;
            ShowData(pageSize, pageIndex);
        }       

        private void ShowData(int pageSize, int pageIndex)
        {
            DataTable dt = new XHCTMS.BLL.Checking().Search(Convert.ToInt32(ViewState["ClassId"]), null, pageSize, pageIndex);                      
            this.Checkinginfo.DataSource = dt;
            Checkinginfo.DataBind();
        }
        private void ShowData(string time, int pageSize, int pageIndex)
        {
            DataTable dt = new XHCTMS.BLL.Checking().Search(Convert.ToInt32(ViewState["ClassId"]), time, pageSize, pageIndex);
            this.Checkinginfo.DataSource = dt;
            Checkinginfo.DataBind();
        }

        private int ChangePageSize()
        {
            return Convert.ToInt32(ViewState["PageSize"]);
        }
        private int ChangePageIndex()
        {
            return Convert.ToInt32(ViewState["PageIndex"]);
        }

        private void PageChange(bool IsFirst)
        {
            try
            {
                int maxPage = Convert.ToInt32(ViewState["MaxPage"]);                
                List<string> page = new List<string> { };
                for (int i = 1; i <= maxPage; ++i)
                {
                    page.Add(i.ToString() + "/" + maxPage.ToString());
                }
                this.ddlPage.DataSource = page;
                this.ddlPage.DataBind();
                if (IsFirst == true)
                {
                    this.ddlPage.SelectedIndex = ChangePageIndex() - 1;
                }
                else
                {
                    this.ddlPage.SelectedIndex = 0;
                    ViewState["PageIndex"] = 1;
                }
            }
            catch
            {
            }
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int pageSize = Convert.ToInt32(this.ddlPageSize.Text);
                ViewState["PageSize"] = pageSize;
                int size = Convert.ToInt32(ViewState["Count"]);
                if (size / ChangePageSize() < 1)
                {
                    ViewState["MaxPage"] = 1;
                }
                else
                {
                    ViewState["MaxPage"] = size % ChangePageSize() == 0 ? size / ChangePageSize() : size / ChangePageSize() + 1;
                }               
                ShowData(pageSize, 1);
                PageChange(false);
            }
            catch
            {
            }
        }
    }
}